package com.shwread.android.ui.page;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
import com.amap.api.location.AMapLocationListener;
import com.ciko.code.bean.PerformanceAddress;
import com.ciko.code.http.BaseResponse;
import com.ciko.code.http.Req.AttendBackReq;
import com.ciko.code.http.Req.AttendReachReq;
import com.ciko.code.http.Req.GetAttendByUserEty;
import com.ciko.code.http.Req.GetAttendByUserReq;
import com.ciko.code.http.Req.QryPerformanceAddressListEty;
import com.ciko.code.http.Req.QryPerformanceAddressListReq;
import com.ciko.code.utils.DeviceUtil;
import com.humanres.activity.ResCheckActivity;
import com.humanres.activity.ResGradeActivity;
import com.humanres.activity.ResMoreActivity;
import com.humanres.activity.ResOtherActivity;
import com.mywork.WorkUIKit;
import com.mywork.activity.PollSurveyActivity;
import com.mywork.activity.ResRemarkActivity;
import com.mywork.activity.WorkMeetingActivity;
import com.mywork.activity.WorkPlaneActivity;
import com.shwread.android.qysw.activity.BaseBusinessActivity;
import com.shwread.android.qysw.activity.BusinessTripActivity;
import com.shwread.android.qysw.activity.FieldAttendanceActivity;
import com.shwread.android.qysw.activity.MainActivity;
import com.shwread.android.qysw.activity.NewLoginActivity;
import com.shwread.android.qysw.activity.ReLocationActivity;
import com.shwread.android.qysw10000227.R;
import com.shwread.android.ui.customview.GridViewCustom;
import com.shwread.android.ui.dialog.CommonDialog;
import com.shwread.android.ui.dialog.NoNetDialog;
import com.shwread.android.ui.widget.LoadingView;
import com.shwread.httpsdk.util.NetUtil;
import com.tgx.tina.android.ipc.framework.IUIPage;

import org.geometerplus.android.util.QyreaderPerferencesUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import base.tina.core.task.infc.ITaskResult;
import logic.bean.MyWorkGridViewBean;
import logic.constants.Const;
import logic.constants.DefaultConsts;
import logic.util.DateUtil;
import logic.util.Util;
import logic.util.logutil.LogUtil;


/**
 * 项目名称：association
 * 包名称：com.shwread.android.ui.page
 * 类描述：
 * 创建人： wayne
 * 创建时间 2016/1/28 2:51.
 */
public class MyWorkPage extends AbstractUIPage<MainActivity> implements View.OnClickListener,
		AMapLocationListener {

	private static MyWorkPage instance;
	private static double EARTH_RADIUS = 6378.137;
	ArrayList<MyWorkGridViewBean> datas;
	QryPerformanceAddressListEty addressListEty;
	private TextView tvTitle;
	private ImageView ivPersion;
	private Button btnBack;
	// private View rect_checkRes;
	// private View rect_leaveRes;
	//private View rect_outRes;
	//private View rect_jiaBanRes;
	//    private View rect_planRes;
	//    private View rect_expenseRes;
	//    private View rect_gradeRes;
	//    private View rect_meetingRes;
	//    private View rect_voteRes;
	//    private View rect_otherRes;
	private View rect_QiandaoCheck;
	private View rect_QiantuiCheck;
	//    private Button ivQiandaoCheck;
	//    private Button ivQiantuiCheck;
	private TextView tvQiandaoDay;
	private TextView tvNoQiandaoDay;
	private TextView dateToDay;
	//    private TextView timeToDay;
	private TextView tvAttendDay;
	private TextView tvAttendStatus;
	private TextView tvQiandaoTimeCheck;
	private TextView tvQiandaoStateCheck;
	private TextView tvQiantuiTimeCheck;
	private TextView tvQiantuiStateCheck;
	private TextView tvAdressCheck;
	private ImageView remarksIcon1;
	private ImageView remarksIcon2;
	private LoadingView loadingView;
	private LinearLayout loadFail;
	private ScrollView workSv;
	private AMapLocationClient locationClient = null;
	private AMapLocationClientOption locationOption = null;
	private AMapLocation location = null;
	private boolean needManulLocation = false;
	//许克烽
	private GridViewCustom grid_view;
	private GetAttendByUserEty getAttendByUser;
	private String reason = "";
	private CommonDialog gpsDialog;
	private ProgressDialog progressDialog;
	private boolean isLocated = false;//判断已定位
	private boolean isGetaddressListEty = false;//判断已获取后台返回值addressListEty
	private int CLICKTYPE = 0;     //1点击的是签退，2是签到
	private Thread thread;
	private CommonDialog dialog;
	private AMapLocation aMapLocation;
	private int attendType = 0;
	private SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");

	public MyWorkPage(MainActivity mainActivity) {
		super(mainActivity);
	}

	public static MyWorkPage getInstance(MainActivity t) {
		if (instance == null) {
			instance = new MyWorkPage(t);
		} else {
			instance.setActivity(t);
		}
		return instance;
	}

	private static double rad(double d) {
		return d * Math.PI / 180.0;
	}

	public static void destroy() {
		if (instance != null) {
			instance.dispose();
			instance = null;
		}
	}

	public static String getChineseWeekday(Calendar paramCalendar) {
		switch (paramCalendar.get(Calendar.DAY_OF_WEEK)) {
			case 1:
				return "星期日";
			case 2:
				return "星期一";
			case 3:
				return "星期二";
			case 4:
				return "星期三";
			case 5:
				return "星期四";
			case 6:
				return "星期五";
			case 7:
				return "星期六";
		}
		return "未知";
	}

	public static String getChineseDate(Calendar paramCalendar) {
		int i = paramCalendar.get(Calendar.YEAR);
		int j = paramCalendar.get(Calendar.MONTH);
		int k = paramCalendar.get(Calendar.DAY_OF_MONTH);
		StringBuffer localStringBuffer = new StringBuffer();
		localStringBuffer.append(i).append("/").append(j + 1).append("/").append(k);
		return localStringBuffer.toString();
	}

	/**
	 * 根据定位结果返回定位信息的字符串
	 *
	 * @param location
	 * @return
	 */
	public synchronized static String getLocationStr(AMapLocation location) {
		if (null == location) {
			return null;
		}
		StringBuffer sb = new StringBuffer();
		//errCode等于0代表定位成功，其他的为定位失败，具体的可以参照官网定位错误码说明
		if (location.getErrorCode() == 0) {
			sb.append("定位成功" + "\n");
			sb.append("定位类型: " + location.getLocationType() + "\n");
			sb.append("经    度    : " + location.getLongitude() + "\n");
			sb.append("纬    度    : " + location.getLatitude() + "\n");
			sb.append("精    度    : " + location.getAccuracy() + "米" + "\n");
			sb.append("提供者    : " + location.getProvider() + "\n");

			if (location.getProvider().equalsIgnoreCase(android.location.LocationManager.GPS_PROVIDER)) {
				// 以下信息只有提供者是GPS时才会有
				sb.append("速    度    : " + location.getSpeed() + "米/秒" + "\n");
				sb.append("角    度    : " + location.getBearing() + "\n");
				// 获取当前提供定位服务的卫星个数
				sb.append("星    数    : " + location.getSatellites() + "\n");
			} else {
				// 提供者是GPS时是没有以下信息的
				sb.append("国    家    : " + location.getCountry() + "\n");
				sb.append("省            : " + location.getProvince() + "\n");
				sb.append("市            : " + location.getCity() + "\n");
				sb.append("城市编码 : " + location.getCityCode() + "\n");
				sb.append("区            : " + location.getDistrict() + "\n");
				sb.append("区域 码   : " + location.getAdCode() + "\n");
				sb.append("地    址    : " + location.getAddress() + "\n");
				sb.append("兴趣点    : " + location.getPoiName() + "\n");
			}
		} else {
			//定位失败
			sb.append("定位失败" + "\n");
			sb.append("错误码:" + location.getErrorCode() + "\n");
			sb.append("错误信息:" + location.getErrorInfo() + "\n");
			sb.append("错误描述:" + location.getLocationDetail() + "\n");
		}
		return sb.toString();
	}

	@Override
	public <E extends Activity> View createView(E e, int i) {

		curMyView = bActivity.getLayoutInflater().inflate(R.layout.fragment_res, null);

		initGpsDialog();
		QyreaderPerferencesUtil util = QyreaderPerferencesUtil.getInstance(context, DefaultConsts.FIRST_TAG);
		if (!Util.gpsIsOpen(bActivity) && util.getShowGpsOpen()) {
			//gpsDialog.show();
			util.setShowGpsOpen(false);
		}
		initTitleBar();
		initView();
		initDatas();
		initListener();
		initAdapter();

		startLocation();

		//获取太保账号信息
		//		doRequestHttp(new UserLoginReq(WorkUIKit.getAccount()));
		new Thread(new Runnable() {
			@Override
			public void run() {
				BaseBusinessActivity.uiHandler.postDelayed(new Runnable() {
					@Override
					public void run() {
						doRequestHttp(new GetAttendByUserReq());
						//获取签到范围
//						doRequestHttp(new QryPerformanceAddressListReq(WorkUIKit.getUser().getBranchNo()));
					}
				}, 1000);
			}
		}).start();
		return curMyView;
	}

	private void initGpsDialog() {
		gpsDialog = new CommonDialog(bActivity) {
			@Override
			protected void clickOk(View v) {
				super.clickOk(v);
				gpsDialog.dismiss();
			}

			@Override
			protected void clickCancel(View v) {
				super.clickCancel(v);
				Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
				bActivity.startActivityForResult(intent, 0);
			}
		};
		gpsDialog.setTvText("打开“GPS”以便准确获取当前位置");
		gpsDialog.setButtonText("取消");
		gpsDialog.setButtonCancelText("设置");
	}

	void startLocation() {
		aMapLocation = null;
		locationClient = new AMapLocationClient(bActivity);
		locationOption = new AMapLocationClientOption();
		// 设置定位模式为高精度模式
		locationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
		// 设置定位监听
		locationClient.setLocationListener(this);
		// 设置定位参数
		locationOption.setNeedAddress(true);
		locationOption.setInterval(1000 * 60);

		locationClient.setLocationOption(locationOption);
		// 启动定位
		locationClient.startLocation();
	}

	private void initAdapter() {
		grid_view.setAdapter(new GridViewAdapter());
		grid_view.setParameter(4);
	}

	private void initTitleBar() {
		tvTitle = (TextView) curMyView.findViewById(R.id.common_header_name);
		tvTitle.setText(R.string.maintab_work);
		ivPersion = (ImageView) curMyView.findViewById(R.id.common_header_persion);
		ivPersion.setVisibility(View.VISIBLE);
		btnBack = (Button) curMyView.findViewById(R.id.common_header_back);
		btnBack.setVisibility(View.GONE);
	}

	protected void initView() {
		Activity activity = bActivity;
		while (activity.getParent() != null) {
			activity = activity.getParent();
		}

		progressDialog = new ProgressDialog(activity);
		progressDialog.setCancelable(true);
		progressDialog.setMessage("正在定位");

		dialog = new CommonDialog(bActivity) {
			@Override
			protected void clickOk(View v) {
				super.clickOk(v);
			}
		};
		dialog.setTvText("获取当前位置失败，请稍后重试");
		dialog.setButtonText("确定");
		dialog.setBtnCancelGone();
		//加载超时判断
		thread = new Thread(new Runnable() {
			@Override
			public void run() {
				BaseBusinessActivity.uiHandler.postDelayed(new Runnable() {
					@Override
					public void run() {
						if (aMapLocation != null && addressListEty == null) {
							dialog.setTvText("获取当前位置失败，请稍后重试");
							progressDialog.dismiss();
							dialog.show();
							stopLocation();
						}
					}
				}, 10000);
			}
		});
		// rect_checkRes = curMyView.findViewById(R.id.rect_checkRes);
		// rect_leaveRes = curMyView.findViewById(R.id.rect_leaveRes);
		//rect_outRes = curMyView.findViewById(R.id.rect_outRes);
		//rect_jiaBanRes = curMyView.findViewById(R.id.rect_jiabanRes);
		//        rect_planRes = curMyView.findViewById(R.id.rect_planeWork);
		//        rect_expenseRes = curMyView.findViewById(R.id.rect_expenseRes);
		//        rect_gradeRes = curMyView.findViewById(R.id.rect_gradeRes);
		//        rect_meetingRes = curMyView.findViewById(R.id.rect_meetingRes);
		//        rect_voteRes = curMyView.findViewById(R.id.rect_voteRes);
		//        rect_otherRes = curMyView.findViewById(R.id.rect_otherRes);

		//        this.tvAdressCheck = (TextView) curMyView.findViewById(R.id.tvAdressCheck);
		tvAttendDay = (TextView) curMyView.findViewById(R.id.tvKaoqinDialy);
		tvAttendStatus = (TextView) curMyView.findViewById(R.id.tvKaoqinStatus);
		this.tvQiantuiStateCheck = (TextView) curMyView.findViewById(R.id.tvQiantuiStateCheck);
		this.tvQiantuiTimeCheck = (TextView) curMyView.findViewById(R.id.tvQiantuiTimeCheck);
		this.tvQiandaoStateCheck = (TextView) curMyView.findViewById(R.id.tvQiandaoStateCheck);
		this.tvQiandaoTimeCheck = (TextView) curMyView.findViewById(R.id.tvQiandaoTimeCheck);
		//        this.timeToDay = (TextView) curMyView.findViewById(R.id.timeToDay);
		this.dateToDay = (TextView) curMyView.findViewById(R.id.dateToDay);
		//        this.tvNoQiandaoDay = (TextView) curMyView.findViewById(R.id.tvNoQiandaoDay);
		//        this.tvQiandaoDay = (TextView) curMyView.findViewById(R.id.tvQiandaoDay);
		this.rect_QiandaoCheck = curMyView.findViewById(R.id.llQiandaoCheck);
		this.rect_QiantuiCheck = curMyView.findViewById(R.id.llQiantuiCheck);
		this.remarksIcon1 = (ImageView) curMyView.findViewById(R.id.remarks_icon1);
		this.remarksIcon2 = (ImageView) curMyView.findViewById(R.id.remarks_icon2);

		grid_view = (GridViewCustom) curMyView.findViewById(R.id.grid_view);
		grid_view.setSelector(new ColorDrawable(Color.TRANSPARENT));

		loadingView = (LoadingView) curMyView.findViewById(R.id.loading_my_work);
		loadingView.setLoadingAnim(R.drawable.home_progress);
		loadingView.changeLoadingStatus(0);
		loadFail = (LinearLayout) curMyView.findViewById(R.id.ll_none_things);
		workSv = (ScrollView) curMyView.findViewById(R.id.my_work_sv);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		tvAttendDay.setText(bActivity.getString(R.string.attend_daily, sdf.format(new Date())));
		GetAttendByUser();
	}

	void initDatas() {
		Calendar calendar = Calendar.getInstance(Locale.CANADA);

		dateToDay.setText(getChineseWeekday(calendar) + " " + getChineseDate(calendar));
		//        timeToDay.setText("当前时间：" + calendar.get(Calendar.HOUR_OF_DAY) + ":" + String.format
		// ("%02d", calendar.get(Calendar.MINUTE)));

		datas = new ArrayList<>();

		datas.add(new MyWorkGridViewBean("课程表", R.drawable.icon_res_syllabus, "login"));
		datas.add(new MyWorkGridViewBean("高桥之星", R.drawable.icon_res_star, "login"));
		datas.add(new MyWorkGridViewBean("学生荣誉", R.drawable.icon_res_honour, "login"));
		datas.add(new MyWorkGridViewBean("教育资源", R.drawable.icon_res_resources, "login"));
		datas.add(new MyWorkGridViewBean("考勤管理", R.drawable.icon_res_check, "check"));
		datas.add(new MyWorkGridViewBean("见贤思齐", R.drawable.icon_res_archives, "login"));
		datas.add(new MyWorkGridViewBean("心理健康测评", R.drawable.icon_res_psychometric, "login"));
//		mg = new MyWorkGridViewBean("请假管理", R.drawable.icon_res_leave, "leave");
//		datas.add(mg);
//
//		mg = new MyWorkGridViewBean("出差管理", R.drawable.icon_res_out, "out");
//		datas.add(mg);
//
//		mg = new MyWorkGridViewBean("加班管理", R.drawable.icon_res_jiaban, "jiaban");
//		datas.add(mg);
//
//		mg = new MyWorkGridViewBean("工作计划", R.drawable.icon_work_plane, "plane");
//		datas.add(mg);
//
//		mg = new MyWorkGridViewBean("报销审批", R.drawable.icon_res_expense, "expense");
//		datas.add(mg);
//
//		mg = new MyWorkGridViewBean("绩效查询", R.drawable.icon_res_grade, "grade");
//		datas.add(mg);
////
//		mg = new MyWorkGridViewBean("会议助手", R.drawable.icon_res_meeting2, "meeting2");
//		datas.add(mg);
//
//		mg = new MyWorkGridViewBean("投票调研", R.drawable.icon_res_vote, "vote");
//		datas.add(mg);
//
//		mg = new MyWorkGridViewBean("知识文库", R.drawable.icon_res_other2, "other2");
//		datas.add(mg);
//
//        mg = new MyWorkGridViewBean("外勤签到", R.drawable.icon_field_att, "att");
//        datas.add(mg);
////		mg = new MyWorkGridViewBean("因公外出", R.drawable.icon_field_att, "att");
////		datas.add(mg);
//
//		mg = new MyWorkGridViewBean("公文传阅", R.drawable.icon_doc_read, "document");
//		datas.add(mg);
	}

	protected void initListener() {
		ivPersion.setOnClickListener(this);
		rect_QiantuiCheck.setOnClickListener(this);
		rect_QiandaoCheck.setOnClickListener(this);
		loadFail.setOnClickListener(this);
	}

	@Override
	public View updateView(int i, ITaskResult iTaskResult) {
		return null;
	}

	@Override
	public void notifyView(int i, Bundle bundle) {
		String remark;
		switch (i) {
			case DefaultConsts.MSG_LOCATION_START:

				break;
			case DefaultConsts.MSG_LOCATION_STOP:

				break;
			case DefaultConsts.MSG_LOCATION_FINISH:
				stopLocation();
//                if (needManulLocation) {
//
//                }
				break;
			case DefaultConsts.MSG_OUT_REMARK:
				attendType = 3;
				remark = bundle.getString("content");
//                doRequestHttp(new AttendReachReq(AttendType.out.getValue(), location.getAddress(), location
//                        .getLongitude(), location.getLatitude(), remark, getPhoneId()));
				doRequestHttp(new AttendReachReq(attendType, location.getAddress(), location
						.getLongitude(), location.getLatitude(), remark, getPhoneId()));
				break;
//            case DefaultConsts.MSG_LEAVE_REMARK:
//                remark = bundle.getString("content");
////                reason = "签到不成功/请假";
//                reason = "签到不成功";
//                doRequestHttp(new AttendReachReq(AttendType.leave.getValue(), location.getAddress(),
//                        location.getLongitude(), location.getLatitude(), remark, getPhoneId()));
//                break;
//            case DefaultConsts.MSG_OTHER_REMARK:
//                remark = bundle.getString("content");
//                reason = "签到不成功";
////                reason = "签到不成功/其它";
//                doRequestHttp(new AttendReachReq(AttendType.other.getValue(), location.getAddress(),
//                        location.getLongitude(), location.getLatitude(), remark, getPhoneId()));
//                break;
			case DefaultConsts.MSG_OUT_BACKREMARK:
				attendType = 3;
				remark = bundle.getString("content");
				doRequestHttp(new AttendBackReq(attendType, location.getAddress(), location
						.getLongitude(), location.getLatitude(), remark, getPhoneId()));
				break;
//            case DefaultConsts.MSG_LEAVE_BACKREMARK:
//                reason = "签退不成功";
////                reason = "签退不成功/请假";
//                remark = bundle.getString("content");
//                doRequestHttp(new AttendBackReq(AttendType.leave.getValue(), location.getAddress(),
//                        location.getLongitude(), location.getLatitude(), remark, getPhoneId()));
//                break;
//            case DefaultConsts.MSG_OTHER_BACKREMARK:
//                reason = "签退不成功";
////                reason = "签退不成功/其它";
//                remark = bundle.getString("content");
//                doRequestHttp(new AttendBackReq(AttendType.other.getValue(), location.getAddress(),
//                        location.getLongitude(), location.getLatitude(), remark, getPhoneId()));
//                break;
		}
	}

	@Override
	public int enter(IUIPage<?> iuiPage) {
		GetAttendByUser();
		return 0;
	}

	void GetAttendByUser() {
		doRequestHttp(new GetAttendByUserReq());
	}

	@Override
	public int leave(IUIPage<?> iuiPage) {
		if (null != locationClient) {
			/**
			 * 如果AMapLocationClient是在当前Activity实例化的，
			 * 在Activity的onDestroy中一定要执行AMapLocationClient的onDestroy
			 */
			locationClient.onDestroy();
			locationClient = null;
			locationOption = null;
		}
		return 0;
	}

	public void setRefresh() {
		doRequestHttp(new GetAttendByUserReq());
	}

	@Override
	public void setStatus(int i) {

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.ll_none_things:
				GetAttendByUser();
				break;
			case R.id.common_header_persion:
				bActivity.goPersonalActivity();
				break;
			case R.id.llQiandaoCheck:
				signManipulate(2);
				break;
			case R.id.llQiantuiCheck:
				signManipulate(1);
				break;
		}
	}

	/**
	 * 进行签到签退执行的操作
	 */
	private void signManipulate(int i) {
		addressListEty = null;
		isGetaddressListEty = false;
		if (!NetUtil.isNetworkConnected(bActivity)) {
			new NoNetDialog(bActivity).show();
			return;
		}
		if (!Util.gpsIsOpen(bActivity)) {
			gpsDialog.show();
			return;
		}
		if (WorkUIKit.getUser().getBranchNo().equals("")) {
			Util.showToast(bActivity, R.string.params_less_tip);
			return;
		}
		CLICKTYPE = i;
		progressDialog.show();
		doRequestHttp(new QryPerformanceAddressListReq(WorkUIKit.getUser().getBranchNo()));
		thread.run();
		isLocated = false;
		startLocation();
	}

	private void clickQiantui() {
		if (!checkLocationValid()) {

			DialogShow();

//            new AttendOutDialog(bActivity, new AttendOutDialog.SimpleOutListener() {
//                @Override
//                public void out() {
//                    toResRemarkForResult("出差", Const.ATTEND_REACH);
//                }
//
//                @Override
//                public void leave() {
//                    toResRemarkForResult("请假", Const.ATTEND_REACH);
//                }
//
//                @Override
//                public void other() {
//                    toResRemarkForResult("其他", Const.ATTEND_REACH);
//                }
//            }).show();
//            Util.showToast(bActivity, "签退");
//            doRequestHttp(new AttendBackReq(4, location.getAddress(), location.getLongitude(), location
// .getLatitude(), ""));
		} else {
			String time1 = addressListEty.getEndTime();
			String time2 = DateUtil.getFormatedDateTime(System.currentTimeMillis());
			int min1 = Integer.valueOf(time1.substring(0, 2)) * 60 + Integer.valueOf(time1.substring(3, 5));
			int min2 = Integer.valueOf(time2.substring(0, 2)) * 60 + Integer.valueOf(time2.substring(3, 5));

			if (min1 - 10 > min2 && checkLocationValid()) {
				CommonDialog commonDialog = new CommonDialog(bActivity) {
					@Override
					protected void clickOk(View v) {
						super.clickOk(v);
						doRequestHttp(new AttendBackReq(4, location.getAddress(), location.getLongitude(),
								location.getLatitude(), "", getPhoneId()));
					}
				};
				commonDialog.setTvText("现在是早退时间，是否确定签退？");
				commonDialog.show();
			} else {
				doRequestHttp(new AttendBackReq(4, location.getAddress(), location.getLongitude(), location
						.getLatitude(), "", getPhoneId()));
			}
		}
	}

	private void DialogShow() {
		dialog = new CommonDialog(bActivity) {
			@Override
			protected void clickOk(View v) {
				Intent intent = new Intent(context, ResRemarkActivity.class);
				intent.putExtra("title", "备注原因");
				bActivity.startActivityForResult(intent, Const.ATTEND_QUIT);
				dialog.dismiss();
			}

			@Override
			protected void clickCancel(View v) {
				dialog.dismiss();
				Intent intent = new Intent(bActivity, ReLocationActivity.class);
				bActivity.startActivity(intent);
			}
		};
		dialog.setButtonText("备注原因");//----ok
		dialog.setButtonCancelText("重新定位");//--------cancel
		dialog.setTvText("当前定位位置不在考勤范围内，请重新定位或选择原因");
		dialog.show();
	}

	//31.1506170000,116.9610680000
	private void clickQiandao() {

		if (checkLocationValid()) {
			//Util.showToast(bActivity, "签到");
			attendType = 0;
			reason = "";
//            doRequestHttp(new AttendReachReq(AttendType.normal.getValue(), location.getAddress(), location
//                    .getLongitude(), location.getLatitude(), "", getPhoneId()));
			doRequestHttp(new AttendReachReq(attendType, location.getAddress(), location
					.getLongitude(), location.getLatitude(), "", getPhoneId()));
		} else {
			dialog = new CommonDialog(bActivity) {
				@Override
				protected void clickOk(View v) {
					Intent intent = new Intent(context, ResRemarkActivity.class);
					intent.putExtra("title", "备注原因");
					bActivity.startActivityForResult(intent, Const.ATTEND_REACH);
					dialog.dismiss();
                   /* new AttendOutDialog(bActivity, new SimpleOutListener() {
                        @Override
                        public void out() {
                            toResRemarkForResult("出差", Const.ATTEND_REACH);
                            //出差签到
                        }

                        @Override
                        public void leave() {
                            toResRemarkForResult("请假", Const.ATTEND_REACH);
                        }

                        @Override
                        public void other() {
                            toResRemarkForResult("其他", Const.ATTEND_REACH);
                        }
                    }).show();
                    dialog.dismiss();*/
				}

				@Override
				protected void clickCancel(View v) {
					dialog.dismiss();
					Intent intent = new Intent(bActivity, ReLocationActivity.class);
					bActivity.startActivity(intent);
				}
			};
			dialog.setButtonText("备注原因");//----ok
			dialog.setCloseBtnVisiable();
			dialog.setButtonCancelText("重新定位");//--------cancel
			dialog.setTvText("当前定位位置不在考勤范围内，请重新定位或选择原因");
			dialog.show();
		}
	}

	private void toResRemarkForResult(String title, int ATTEND) {
		Intent intent = new Intent(bActivity, ResRemarkActivity.class);
		intent.putExtra("title", title);
		bActivity.startActivityForResult(intent, ATTEND);
	}

	boolean checkLocationValid() {
		boolean result = false;
		if (null == addressListEty) {
			doRequestHttp(new QryPerformanceAddressListReq(WorkUIKit.getUser().getBranchNo()));
		} else {
			if (!addressListEty.getPerformaceList().isEmpty()) {
				for (PerformanceAddress address : addressListEty.getPerformaceList()) {
//                    String dis = getDistance(String.valueOf(31.1506170000), String.valueOf(116
// .9610680000), address.getLat(), address.getLon());
					String dis = getDistance(String.valueOf(location.getLatitude()), String.valueOf
							(location.getLongitude()), address.getLat(), address.getLon());
					LogUtil.d("location", dis);
//					if ((Long.parseLong(dis) * 1000 - Long.parseLong(address.getDistance())) < 0)
					if ((Double.valueOf(dis) * 1000 - Double.valueOf(address.getDistance())) < 0) {
						result = true;
						break;
					}
				}
			}
		}
		LogUtil.d("location", "" + result);
		return result;
	}

	/**
	 * 根据两个位置的经纬度，来计算两地的距离（单位为KM）
	 * 参数为String类型
	 *
	 * @param lat1Str 用户经度
	 * @param lng1Str 用户纬度
	 * @param lat2Str 商家经度
	 * @param lng2Str 商家纬度
	 * @return
	 */
	public String getDistance(String lat1Str, String lng1Str, String lat2Str, String lng2Str) {
		Double lat1 = Double.parseDouble(lat1Str);
		Double lng1 = Double.parseDouble(lng1Str);
		Double lat2 = Double.parseDouble(lat2Str);
		Double lng2 = Double.parseDouble(lng2Str);

		double radLat1 = rad(lat1);
		double radLat2 = rad(lat2);
		double difference = radLat1 - radLat2;
		double mdifference = rad(lng1) - rad(lng2);
		double distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(difference / 2), 2) + Math.cos(radLat1)
				* Math.cos(radLat2) * Math.pow(Math.sin(mdifference / 2), 2)));
		distance = distance * EARTH_RADIUS;
//		distance = Math.round(distance * 10000) / 10000;
		String distanceStr = distance + "";

//		distanceStr = distanceStr.substring(0, distanceStr.indexOf("."));

		return distanceStr;
	}

	private void gotoMoreActivity(String title) {
		Intent intent = new Intent(getContext(), ResMoreActivity.class);
		intent.putExtra("title", title);
		bActivity.startActivity(intent);
	}

	@Override
	public void onResponseFail(String tag, int resultCode) {
		super.onResponseFail(tag, resultCode);
		if (resultCode == 4001) {

			rect_QiantuiCheck.setVisibility(View.VISIBLE);
			rect_QiandaoCheck.setVisibility(View.GONE);
		} else if (resultCode == 8000) {
//            Calendar calendar = Calendar.getInstance(Locale.CANADA);
//            SimpleDateFormat sdf = new SimpleDateFormat("yy/MM/dd");
//            tvAttendDay.setText(bActivity.getString(R.string.attend_daily, sdf.format(new Date())));
			tvAttendStatus.setText(bActivity.getString(R.string.empty));
			tvQiandaoTimeCheck.setText("未签到");
			tvQiantuiTimeCheck.setText("未签退");
			tvQiandaoStateCheck.setText("");
			tvQiandaoStateCheck.setText("");

			rect_QiantuiCheck.setVisibility(View.GONE);
			rect_QiandaoCheck.setVisibility(View.VISIBLE);
		} else if (resultCode == 4003) {
			Log.i("TAG", tag);
			String str = tag.equals("AttendReachReq") ? "签到" : "签退";
			AlertDialog.Builder builder = new AlertDialog.Builder(context);
			builder.setMessage("您更换了手机，请联系管理员后再" + str);
			builder.setPositiveButton("确定", null);
			builder.show();
		} else {
			loadingView.setVisibility(View.GONE);
//			loadFail.setVisibility(View.VISIBLE);
//			workSv.setVisibility(View.GONE);
		}

		if (tag.equals("QryPerformanceAddressListReq")) {
			progressDialog.dismiss();
			CLICKTYPE = 0;
			if (resultCode == 9999) {
				DialogShow();
			}
		}
	}

	@Override
	public void onResponseSuccess(final BaseResponse<?> response, final String tag) {
		super.onResponseSuccess(response, tag);
		Log.e("tag", tag);
		switch (tag) {
			case "GetAttendByUserReq":
				loadingView.setVisibility(View.GONE);
				loadFail.setVisibility(View.GONE);
				workSv.setVisibility(View.VISIBLE);
				final GetAttendByUserEty getAttendByUserEty = (GetAttendByUserEty) response
						.getReturnObject();
				getAttendByUser = getAttendByUserEty;
				qianDaoQianTuiBut(getAttendByUserEty);
				String attendType = getAttendByUser.getAttendType();
				String attendTypeName = "";
				switch (attendType) {
					case "1":
						attendTypeName = "出差";
						break;
					case "2":
						attendTypeName = "请假";
						break;
					case "3":
						attendTypeName = "其他";
						break;
					case "4":
						attendTypeName = "因公外出";
						break;
				}
				if (getAttendByUserEty.getReachTimeStr().length() > 0) {
					tvQiandaoTimeCheck.setVisibility(View.VISIBLE);
					tvQiandaoStateCheck.setVisibility(View.VISIBLE);
					tvQiantuiTimeCheck.setVisibility(View.VISIBLE);
					tvQiantuiStateCheck.setVisibility(View.VISIBLE);
					switch (getAttendByUserEty.getAttendType()) {
						case "0":
							//正常的签到签退逻辑
							normalAttend(getAttendByUserEty);
							normalQuit(getAttendByUserEty);
							break;
						case "1":
						case "2":
						case "3":
						case "4":
							if (getAttendByUserEty.getTripLeaveType() == 0) {
								switch (getAttendByUserEty.getApprovalStatus()) {
									case 1:
										if (getAttendByUserEty.getQuitTimeStr()
												.length() > 0) {
											tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
													.getReachTimeStr().split(" ")[1]);
											tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty
													.getQuitTimeStr().split(" ")[1]);
											tvQiandaoStateCheck.setText(attendTypeName + "/审批中");
											tvQiantuiStateCheck.setText(getAttendByUserEty.getQuitTypeName
													() + "/审批中");
										} else {
											tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
													.getReachTimeStr().split(" ")[1]);
											tvQiantuiTimeCheck.setText("未签退");
											tvQiandaoStateCheck.setText(attendTypeName + "/审批中");
											tvQiantuiStateCheck.setText("");
											if (getAttendByUserEty.getAttendType().equals("4")){
												rect_QiandaoCheck.setVisibility(View.GONE);
												rect_QiantuiCheck.setVisibility(View.VISIBLE);
												rect_QiantuiCheck.setEnabled(true);
												tvQiantuiStateCheck.setText(attendTypeName + "/待签到");
												rect_QiantuiCheck.setBackgroundResource(R.drawable.icon_qiantui);
											}
										}
										break;
									case 2:
										tvQiandaoTimeCheck.setText("已签到 ");
										tvQiandaoStateCheck.setText(attendTypeName);
										if (getAttendByUserEty.getQuitTimeStr()
												.length() > 0) {
											tvQiantuiTimeCheck.setText("已签退 ");
											tvQiantuiStateCheck.setText(attendTypeName);
											rect_QiantuiCheck.setEnabled(false);
											rect_QiantuiCheck.setBackgroundResource(R.drawable.qiantui_bg);
										} else {
											tvQiantuiTimeCheck.setText("未签退");
											tvQiantuiStateCheck.setText("");
										}
										break;
									case 3:
										normalAttend(getAttendByUserEty);
										normalQuit(getAttendByUserEty);
										break;
									case 4:
										if (getAttendByUserEty.getQuitTimeStr()
												.length() > 0) {
											tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
													.getReachTimeStr().split(" ")[1]);
											tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty
													.getQuitTimeStr().split(" ")[1]);
											tvQiandaoStateCheck.setText(attendTypeName + "/待签到");
											tvQiantuiStateCheck.setText(getAttendByUserEty.getQuitTypeName
													() + "/待签到");
										} else {
											tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
													.getReachTimeStr().split(" ")[1]);
											tvQiantuiTimeCheck.setText("未签退");
											tvQiandaoStateCheck.setText(attendTypeName + "/待签到");
										}

										break;
								}
							} else if (getAttendByUserEty.getTripLeaveType() == 1) {
								switch (getAttendByUserEty.getApprovalStatus()) {
									case 1:
										tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
												.getReachTimeStr().split(" ")[1]);
										tvQiandaoStateCheck.setText(attendTypeName + "/审批中");
										normalQuit(getAttendByUserEty);
										break;
									case 2:
										tvQiandaoTimeCheck.setText("已签到 ");
										tvQiandaoStateCheck.setText(attendTypeName);
										normalQuit(getAttendByUserEty);
										break;
									case 3:
										normalAttend(getAttendByUserEty);
										normalQuit(getAttendByUserEty);
										break;
									case 4:
										tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
												.getReachTimeStr().split(" ")[1]);
										tvQiandaoStateCheck.setText(attendTypeName + "/待签到");
										normalQuit(getAttendByUserEty);
										break;
								}
							} else {//请下午假
								switch (getAttendByUserEty.getApprovalStatus()) {
									case 1://审批中
										normalAttend(getAttendByUserEty);
										tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty
												.getQuitTimeStr().split(" ")[1]);
										tvQiantuiStateCheck.setText(attendTypeName + "/审批中");
										break;
									case 2://审批通过
										normalAttend(getAttendByUserEty);
										tvQiantuiTimeCheck.setText("已签退 ");
										tvQiantuiStateCheck.setText(attendTypeName);
										break;
									case 3://审批不同意
										normalAttend(getAttendByUserEty);
										normalQuit(getAttendByUserEty);
										break;
									case 4://审批中
										normalAttend(getAttendByUserEty);
										tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty
												.getQuitTimeStr().split(" ")[1]);
										tvQiantuiStateCheck.setText(attendTypeName + "/待签到");
										break;
								}
							}
							break;
					}
				} else {//没有签到的时间reachTimeStr=""
					tvQiandaoTimeCheck.setVisibility(View.VISIBLE);
					tvQiandaoStateCheck.setVisibility(View.VISIBLE);
					tvQiantuiTimeCheck.setVisibility(View.VISIBLE);
					tvQiantuiStateCheck.setVisibility(View.VISIBLE);
					if (getAttendByUserEty.getQuitTimeStr().length() == 0) {//未签退
						tvQiantuiTimeCheck.setText("未签退");
						tvQiandaoTimeCheck.setText("未签到");
						tvQiandaoStateCheck.setText("");
						tvQiantuiStateCheck.setText("");
						if (getAttendByUserEty.getApprovalStatus()==4||getAttendByUserEty.getApprovalStatus()==1){
							switch (getAttendByUserEty.getTripLeaveType()){
								case 0:
									tvQiandaoStateCheck.setText(attendTypeName+"/审批中");
									tvQiantuiStateCheck.setText(attendTypeName+"/审批中");
									if (getAttendByUserEty.getAttendType().equals("4")&&getAttendByUserEty.getApprovalStatus()==4){
										tvQiantuiStateCheck.setText(attendTypeName+"/待签到");
										tvQiandaoStateCheck.setText(attendTypeName+"/待签到");
									}
									break;
								case 1:
									tvQiandaoStateCheck.setText(attendTypeName+"/审批中");
									tvQiantuiStateCheck.setText("");
									if (getAttendByUserEty.getAttendType().equals("4")&&getAttendByUserEty.getApprovalStatus()==4){
										tvQiantuiStateCheck.setText("");
										tvQiandaoStateCheck.setText(attendTypeName+"/待签到");
									}
									break;
								case 2:
									tvQiandaoStateCheck.setText("");
									tvQiantuiStateCheck.setText(attendTypeName+"/审批中");
//                                    tvQiandaoTimeCheck.setText("未签到");
//                                    tvQiantuiTimeCheck.setText("未签退");
									if (getAttendByUserEty.getAttendType().equals("4")&&getAttendByUserEty.getApprovalStatus()==4){
										tvQiandaoStateCheck.setText("");
										tvQiantuiStateCheck.setText(attendTypeName+"/待签到");
									}
									break;
							}
						}
					} else {//已经签退
						switch (getAttendByUserEty.getAttendType()) {
							case "0":
								normalAttend(getAttendByUserEty);
								normalQuit(getAttendByUserEty);
								break;
							case "1":
							case "2":
							case "3":
							case "4":
								switch (getAttendByUserEty.getApprovalStatus()) {
									case 1:
										normalAttend(getAttendByUserEty);
										tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty
												.getQuitTimeStr().split(" ")[1]);
										tvQiantuiStateCheck.setText(attendTypeName + "/审批中");
										break;
									case 2:
										normalAttend(getAttendByUserEty);
										tvQiantuiTimeCheck.setText("已签退 ");
										tvQiantuiStateCheck.setText(attendTypeName);
										break;
									case 3:
										normalAttend(getAttendByUserEty);
										normalQuit(getAttendByUserEty);
										break;
									case 4:
										normalAttend(getAttendByUserEty);
										tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty
												.getQuitTimeStr().split(" ")[1]);
										tvQiantuiStateCheck.setText(attendTypeName + "/待签到");
										break;
								}
								break;
						}
					}
				}
				/********************************以下逻辑跟因公外出无关****************************************/
				if (getAttendByUserEty.getApprovalStatus() == 1&&!getAttendByUserEty.getAttendType().equals("4")) {
					if (getAttendByUserEty.getReachTimeStr().length() > 0) {
						tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty.getReachTimeStr().split(" ")
								[1]);
						if (getAttendByUserEty.getQuitTimeStr().length() > 0) {
							tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty.getQuitTimeStr().split("" +
									" ")[1]);
						}
//                        else{
//                            tvQiantuiTimeCheck.setText("未签退");
//                        }
					}
//                    else{
//                        tvQiandaoTimeCheck.setText("未签到");
//                        if (getAttendByUserEty.getQuitTimeStr().length()>0&&getAttendByUserEty
// .getQuitTimeStr()!=null){
//                            tvQiantuiTimeCheck.setText("已签退");
//                        }else{
//                            tvQiantuiTimeCheck.setText("未签退");
//                        }
//                    }
					switch (getAttendByUserEty.getAttendType()) {//审批中的各种情况（请假，出差，因公外出）
						case "0":
							break;
						case "1":
						case "2":
						case "3":
						case "4":
							switch (getAttendByUserEty.getTripLeaveType()) {
								case 0://全天
									tvQiandaoStateCheck.setText(attendTypeName + "/审批中");
									tvQiantuiStateCheck.setText(attendTypeName + "/审批中");
									if (getAttendByUserEty.getReachTimeStr().length() > 0) {
										tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
												.getReachTimeStr().split(" ")[1]);
										rect_QiandaoCheck.setVisibility(View.GONE);
										if (getAttendByUserEty.getQuitTimeStr().length() > 0) {
											tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty
													.getQuitTimeStr().split(" ")[1]);
											rect_QiandaoCheck.setVisibility(View.GONE);
											rect_QiantuiCheck.setVisibility(View.VISIBLE);
											rect_QiantuiCheck.setEnabled(false);
											rect_QiantuiCheck.setBackgroundResource(R.drawable.qiantui_bg);
										} else {
											tvQiantuiTimeCheck.setText("未签退");
											rect_QiandaoCheck.setVisibility(View.GONE);
											rect_QiantuiCheck.setVisibility(View.VISIBLE);
											rect_QiantuiCheck.setEnabled(true);
										}
									} else {
										tvQiandaoTimeCheck.setText("未签到");
										rect_QiandaoCheck.setVisibility(View.VISIBLE);
										rect_QiantuiCheck.setVisibility(View.GONE);
									}
									break;
								case 1://上午
									tvQiandaoStateCheck.setText(attendTypeName + "/审批中");
									if (getAttendByUserEty.getReachTimeStr().length() > 0) {
										tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
												.getReachTimeStr().split(" ")[1]);
										rect_QiandaoCheck.setVisibility(View.GONE);
										if (getAttendByUserEty.getQuitTimeStr().length() > 0) {
											tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty
													.getQuitTimeStr().split(" ")[1]);
											rect_QiandaoCheck.setVisibility(View.GONE);
											rect_QiantuiCheck.setVisibility(View.VISIBLE);
											rect_QiantuiCheck.setEnabled(false);
											rect_QiantuiCheck.setBackgroundResource(R.drawable.qiantui_bg);
										} else {
											tvQiantuiTimeCheck.setText("未签退");
											rect_QiandaoCheck.setVisibility(View.GONE);
											rect_QiantuiCheck.setVisibility(View.VISIBLE);
											rect_QiantuiCheck.setEnabled(true);
											rect_QiantuiCheck.setBackgroundResource(R.drawable.icon_qiantui);
										}
									} else {
										tvQiandaoTimeCheck.setText("未签到");
										rect_QiandaoCheck.setVisibility(View.VISIBLE);
										rect_QiantuiCheck.setVisibility(View.GONE);

									}
									break;
								case 2://下午
									tvQiantuiStateCheck.setText(attendTypeName + "/审批中");
									if (getAttendByUserEty.getReachTimeStr().length() > 0) {
										tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
												.getReachTimeStr().split(" ")[1]);
										rect_QiandaoCheck.setVisibility(View.GONE);
										if (getAttendByUserEty.getQuitTimeStr().length() > 0) {
											tvQiantuiTimeCheck.setText("已签退 ");
											rect_QiandaoCheck.setVisibility(View.GONE);
											rect_QiantuiCheck.setVisibility(View.VISIBLE);
											rect_QiantuiCheck.setEnabled(false);
											rect_QiantuiCheck.setBackgroundResource(R.drawable.qiantui_bg);
										} else {
											tvQiantuiTimeCheck.setText("未签退");
											rect_QiandaoCheck.setVisibility(View.GONE);
											rect_QiantuiCheck.setVisibility(View.VISIBLE);
											rect_QiantuiCheck.setEnabled(true);
										}
									} else {
										tvQiandaoTimeCheck.setText("未签到");
										rect_QiandaoCheck.setVisibility(View.VISIBLE);
										rect_QiantuiCheck.setVisibility(View.GONE);
									}
									break;
							}
							break;
					}
				}
				break;
			case "QryPerformanceAddressListReq":
				addressListEty = (QryPerformanceAddressListEty) response.getReturnObject();
				isGetaddressListEty = true;
				if (isLocated) {
					progressDialog.dismiss();
					if (CLICKTYPE == 1) {
						clickQiantui();
					} else if (CLICKTYPE == 2) {
						clickQiandao();
					}
					CLICKTYPE = 0;
				}
				GetAttendByUser();
				break;
			case "AttendReachReq":
				String msg1 = (String) response.getReturnObject();
				Util.showToast(bActivity, msg1);
				//刷新页面
				GetAttendByUser();
				break;
			case "AttendBackReq":
				String msg2 = (String) response.getReturnObject();
				Util.showToast(bActivity, msg2);
				//刷新页面
				GetAttendByUser();
				break;
		}
	}

	public void qianDaoQianTuiBut(GetAttendByUserEty getAttendByUser) {
		rect_QiandaoCheck.setVisibility(View.VISIBLE);
		rect_QiantuiCheck.setVisibility(View.GONE);
//        String reachTime = Long.toString(getAttendByUser.getReachTime());
//        String quitTime = Long.toString(getAttendByUser.getQuitTime());
//        if (reachTime == null || reachTime == "" || reachTime.equals("null")) {
//            rect_QiandaoCheck.setVisibility(View.VISIBLE);
//            rect_QiantuiCheck.setVisibility(View.GONE);
//        } else {
//            rect_QiandaoCheck.setVisibility(View.GONE);
//            if (quitTime == null || quitTime == "" || quitTime.equals("null")) {
//                rect_QiantuiCheck.setVisibility(View.VISIBLE);
//            } else {
//                rect_QiantuiCheck.setVisibility(View.VISIBLE);
//                rect_QiantuiCheck.setBackgroundResource(R.drawable.qiantui_bg);
//                rect_QiantuiCheck.setEnabled(false);
//            }
//        }


		if (getAttendByUser.getReachTimeStr().length() > 0 && getAttendByUser.getReachTimeStr() != null) {
//            tvQiandaoTimeCheck.setText("已签到");
			rect_QiandaoCheck.setVisibility(View.GONE);
			rect_QiantuiCheck.setVisibility(View.VISIBLE);
			if (getAttendByUser.getQuitTimeStr().length() > 0 && getAttendByUser.getQuitTimeStr() != null) {
//                rect_QiandaoCheck.setVisibility(View.VISIBLE);
//                rect_QiantuiCheck.setVisibility(View.GONE);
				rect_QiantuiCheck.setEnabled(false);
				rect_QiantuiCheck.setBackgroundResource(R.drawable.qiantui_bg);
			}
		}
	}

	//正常的签到----已签到过了
	public void normalAttend(GetAttendByUserEty getAttendByUser) {
		tvQiandaoTimeCheck.setVisibility(View.VISIBLE);
		tvQiandaoStateCheck.setVisibility(View.VISIBLE);
		SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
		if (getAttendByUser.getAttendType().equals("0")) {
			if (getAttendByUser.getReachTimeStr().length() > 0) {
				tvQiandaoTimeCheck.setText("已签到 " + df.format(new Date(
						getAttendByUser.getReachTime())));
//                tvQiandaoStateCheck.setText();
			} else {
				tvQiandaoTimeCheck.setText("未签到 ");
			}
		} else {
			if (getAttendByUser.getReachTimeStr().length() > 0) {
				tvQiandaoTimeCheck.setText("已签到 " + df.format(new Date(
						getAttendByUser.getReachTime())));
				rect_QiandaoCheck.setVisibility(View.GONE);
				rect_QiantuiCheck.setVisibility(View.VISIBLE);
				rect_QiantuiCheck.setEnabled(false);
				rect_QiantuiCheck.setBackgroundResource(R.drawable.qiantui_bg);
			} else {
				tvQiandaoTimeCheck.setText("未签到 ");
				tvQiandaoStateCheck.setText("");
				rect_QiandaoCheck.setVisibility(View.VISIBLE);
				rect_QiantuiCheck.setVisibility(View.GONE);
			}
		}
		if (getAttendByUser.getReachType().equals("0")) {
			tvQiandaoStateCheck.setText("签到成功");
		} else if (getAttendByUser.getReachType().equals("1")) {
			tvQiandaoStateCheck.setText("迟到");
		} else if (getAttendByUser.getReachType().equals("3")){
			tvQiandaoStateCheck.setText("其他");
		}
	}

	//正常的签退
	public void normalQuit(GetAttendByUserEty getAttendByUser) {
		tvQiantuiTimeCheck.setVisibility(View.VISIBLE);
		tvQiantuiStateCheck.setVisibility(View.VISIBLE);
		if (getAttendByUser.getQuitTimeStr().length()==0) {
			tvQiantuiTimeCheck.setText("未签退");
			tvQiantuiStateCheck.setText("");
			rect_QiantuiCheck.setEnabled(true);
		} else {
			SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
			tvQiantuiTimeCheck.setText("已签退 " + df.format(new Date(
					getAttendByUser.getQuitTime())));
			if (getAttendByUser.getQuitType().equals("0")) {
				tvQiantuiTimeCheck.setText("已签退 "+getAttendByUser.getQuitTimeStr().split(" ")[1]);
				tvQiantuiStateCheck.setText("正常");
			} else if (getAttendByUser.getQuitType().equals("2")) {
				tvQiantuiStateCheck.setText("早退");
				tvQiantuiTimeCheck.setText("已签退 " + df.format(new Date(
						getAttendByUser.getQuitTime())));
			} else {
				tvQiantuiStateCheck.setText("签退异常");
				tvQiantuiTimeCheck.setText("已签退 " + df.format(new Date(
						getAttendByUser.getQuitTime())));
			}
			rect_QiantuiCheck.setEnabled(false);
			rect_QiantuiCheck.setBackgroundResource(R.drawable.qiantui_bg);
		}
	}

	Drawable getDrawable(int resId) {
		return context.getResources().getDrawable(resId);
	}

	@Override
	public void onLocationChanged(AMapLocation aMapLocation) {
		this.aMapLocation = aMapLocation;
		if (null != aMapLocation) {
			Message msg = Message.obtain();
			msg.obj = aMapLocation;
			msg.what = DefaultConsts.MSG_LOCATION_FINISH;
			bActivity.getUIHandler().sendMessage(msg);
			//            Logger.i(getLocationStr(aMapLocation));
			this.location = aMapLocation;
			isLocated = true;
			if (isGetaddressListEty) {
				progressDialog.dismiss();
				if (CLICKTYPE == 1) {
					clickQiantui();
				} else if (CLICKTYPE == 2) {
					clickQiandao();
				}
				CLICKTYPE = 0;
			}
		} else {
			progressDialog.dismiss();
			CLICKTYPE = 0;
			dialog.show();
		}
	}

	void stopLocation() {
		if (null != locationClient) {
			locationClient.stopLocation();
		}
	}

	/**
	 * 获取手机识别码
	 */
	private String getPhoneId() {
		DeviceUtil deviceUtil = new DeviceUtil(context);
		if (!Util.isEmpty(deviceUtil.getImei())) {
			return deviceUtil.getImei();
		}
		return deviceUtil.getAndroidId();
	}

	class GridViewAdapter
			extends
			BaseAdapter {

		@Override
		public int getCount() {
			return datas.size();
		}

		@Override
		public Object getItem(int position) {
			return datas.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(R.layout.my_work_gridview_child,
						null);
				holder = new ViewHolder();
				holder.img_view = (ImageView) convertView.findViewById(R.id.img_view);
				holder.txt_view = (TextView) convertView.findViewById(R.id.txt_view);
				holder.lin_layout = (LinearLayout) convertView.findViewById(R.id.lin_layout);
				holder.lin_layout.setTag(datas.get(position).getType());

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.txt_view.setText(datas.get(position).getName());
			holder.img_view.setBackgroundResource(datas.get(position).getSource());
			holder.lin_layout.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					String type = datas.get(position).getType();
					switch (type) {
						case "login":
							bActivity.changePage(new LoginViewPage(bActivity), DEFAULT_STATUS);
//							intent2Acticity(NewLoginActivity.class, datas.get(position).getName());
							break;
						case "check":
							intent2Acticity(ResCheckActivity.class, datas.get(position).getName());
							break;
						case "leave":
							gotoMoreActivity("请假管理");
							break;
						case "out":
							gotoMoreActivity("出差管理");
							break;
						case "jiaban":
							gotoMoreActivity("加班管理");
							break;
						case "plane"://工作计划
							Intent intent = new Intent(bActivity, WorkPlaneActivity.class);
							if (null != location)
								intent.putExtra("address", location.getAddress());
							intent.putExtra("title", datas.get(position).getName());
							context.startActivity(intent);
							break;
						case "expense":
							gotoMoreActivity("报销审批");
							break;
						case "grade"://绩效查询
							intent2Acticity(ResGradeActivity.class, datas.get(position).getName());
							break;
						case "meeting2"://会议助手
							intent2Acticity(WorkMeetingActivity.class, datas.get(position).getName());
							break;
						case "vote"://投票调研
							intent2Acticity(PollSurveyActivity.class, datas.get(position).getName());
							break;
						case "other2"://知识文库
							intent2Acticity(ResOtherActivity.class, datas.get(position).getName());
							break;

						case "att"://外勤签到
                            intent2Acticity(FieldAttendanceActivity.class, datas.get(position).getName());
//							intent2Acticity(BusinessTripActivity.class, "因公外出申请");

							break;
						case "document"://公文传阅
							intent2Acticity(ResOtherActivity.class, datas.get(position).getName());
							break;
					}
				}
			});
			return convertView;
		}
	}

	class ViewHolder {
		ImageView img_view;
		TextView txt_view;
		LinearLayout lin_layout;
	}
}